﻿using System.Collections.Generic;
using System.Xml.Linq;

namespace RoAssistant.Web
{
    /// <summary>
    /// 将List信息载入Tci文件
    /// </summary>
    public class ChangeListToTci
    {
        /// <summary>
        /// XDocument形式的临时xml文件
        /// </summary>
        private XDocument _temptciDocument;


        /// <summary>
        /// xml文件的根节点
        /// </summary>
        private XElement _rootElement;

        /// <summary>
        /// 创建tci文件
        /// </summary>
        /// <param name="elementInfos">元素信息List</param>
        /// <param name="name">名称</param>
        public void CreateTempTci(List<WebElement> elementInfos, string name)
        {
            _temptciDocument = new XDocument();

            XNamespace webinfo = "http://tempuri.org/RoTciForWeb.xsd";
            _rootElement = new XElement(webinfo + "ElementInfo", new XAttribute(XNamespace.Xmlns + "webinfo", "http://tempuri.org/RoTciForWeb.xsd"));
            _temptciDocument.Add(_rootElement);
            XElement resElement = new XElement(webinfo + "Resources");
            _rootElement.Add(resElement);
            foreach (WebElement sigElementInfo in elementInfos)
            {
                XElement sigXElement = CreateControlXmlNode(sigElementInfo, webinfo);
                resElement.Add(sigXElement);
            }
            _temptciDocument.Save("C:\\Temp\\" + name + ".tci");
        }


        /// <summary>
        /// 将所有的list内信息拷贝进入tci文件
        /// </summary>
        /// <param name="elementInfo">单个elementinfo</param>
        /// <param name="webinfo">命名空间</param>
        /// <returns></returns>
        private XElement CreateControlXmlNode(WebElement elementInfo, XNamespace webinfo)
        {
            XElement retXElement = new XElement(webinfo + "Webcontrol",
                new XAttribute(webinfo + "ID", elementInfo.Id),
                new XElement(webinfo + "SearchProperty",
                    new XAttribute(webinfo + "Locator", "XPath"), elementInfo.Value));

            return retXElement;
        }
    }
}
